package com.ruoyi.member.service;

import cn.hutool.core.bean.BeanUtil;
import com.easy.query.api.proxy.client.EasyEntityQuery;
import com.ruoyi.common.core.domain.entity.MemberUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.core.domain.MemberUserUpdateReqVo;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MemberUserServiceImpl implements IMemberUserService {
    @Resource
    private EasyEntityQuery easyEntityQuery;

    @Override
    public MemberUser selectAppUserByUserId(Long userId) {
        MemberUser entity = easyEntityQuery.queryable(MemberUser.class).where(m ->m.userId().eq(userId)).firstOrNull();
        return entity;
    }

    @Override
    public List<MemberUser> selectAppUserList(MemberUser appUser) {
        return easyEntityQuery.queryable(MemberUser.class).toList();
    }



    @Override
    public long deleteAppUserByUserIds(Long[] userIds) {
        return easyEntityQuery.deletable(MemberUser.class)
                .where(m -> m.userId().in(userIds))
                .disableLogicDelete()
                .allowDeleteStatement(true).executeRows();
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param userId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public long deleteAppUserByUserId(Long userId) {

        return easyEntityQuery.deletable(MemberUser.class)
                .where(m -> m.userId().eq(userId))
                .disableLogicDelete()
                .allowDeleteStatement(true).executeRows();
    }

    /**
     * 新增用户
     *
     * @param appUser
     * @return
     */
    @Override
    public long insertAppUser(MemberUser appUser) {
        return easyEntityQuery.insertable(appUser).executeRows();
    }

    /**
     * 通过用户名查询用户
     *
     * @param openId 用户名
     * @return 用户对象信息
     */
    @Override
    public MemberUser selectAppUserByOpenId(String openId) {
        return easyEntityQuery.queryable(MemberUser.class)
                .where(m -> m.openId().eq(openId)).firstOrNull();
    }
    @Override
    public MemberUser selectAppUserByUserName(String userName) {
        return easyEntityQuery.queryable(MemberUser.class)
                .where(m -> m.userName().eq(userName)).firstOrNull();
    }

    @Override
    public Boolean regAppUser(MemberUser appUser) {
        String salt = generateSalt();
        MemberUser appUser1 = new MemberUser();
        appUser1.setUserName(appUser.getUserName());
        appUser1.setPassword(DigestUtils.md5DigestAsHex(appUser.getPassword().concat(salt).getBytes()));
        appUser1.setSalt(salt);
        return true;
    }

    /**
     * 修改保存用户信息
     *
     * @param memberUser 用户信息
     * @return 结果
     */
    @Override
    public long updateAppUser(MemberUser memberUser) {
        return  easyEntityQuery.updatable(memberUser).executeRows();
    }

    /**
     * 密码校验
     *
     * @param password 明文
     * @param salt     盐
     * @param hashPwd  密文
     * @return boolean
     */
    public boolean checkPassword(String password, String salt, String hashPwd) {
        return hashPwd.equals(DigestUtils.md5DigestAsHex(password.concat(salt).getBytes()));
    }

    @Override
    public boolean apiUpdateAppUser(MemberUserUpdateReqVo reqVo) {
        MemberUser memberUser = easyEntityQuery.queryable(MemberUser.class)
                .where(m -> m.userId().eq(SecurityUtils.getAppUserId())).firstOrNull();
        BeanUtil.copyProperties(reqVo, memberUser);
        easyEntityQuery.updatable(memberUser).executeRows();
        return true;
    }



    /**
     * 随即生成盐
     * 8位随机数
     *
     * @return salt 盐
     */
    private String generateSalt() {
        return RandomStringUtils.randomAlphanumeric(8);
    }
}
